package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.opensymphony.xwork2.ActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.CommonDict;
import entities.TMail;
import entities.TMerchant;
import interfaces.IMailService;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class MailAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IMailService mailService;

	private TMail mailBean;
	private List<TMail> mailList;

	private String malNo;
	private String malNos;
	private String merNo;
	private String malInfo;
	private String malRead;
	private String malState;
	private Date malDate;
	private Date malDateTop;
	private Date malDateEnd;
	private String malRemark;

	// 展示对象
	public String show() throws Exception {
		if (!mailService.exist(malNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			mailBean = mailService.fetch(malNo);
			return SUCCESS;
		}
	}

	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("merNo", ((TMerchant) ActionContext.getContext().getSession().get("merchant")).getMerNo());
		paramMap.put("malState", "1");

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "malDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, 5));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMail> resultSet = mailService.search(querySet);
		mailList = resultSet.getResultList();
		for (TMail mail : mailList) {
			mail.setMalRead("1");
			mailService.persist(mail);
		}
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("malNo", StringUtil.split(malNo));
		paramMap.put("merNo", merNo);
		paramMap.put("malInfo", malInfo);
		paramMap.put("malRead", malRead);
		paramMap.put("malState", malState);
		paramMap.put("malDate", ParameterUtil.dateCombin(malDateTop, malDateEnd));
		paramMap.put("malRemark", malRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "malDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TMail> resultSet = mailService.search(querySet);
		mailList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("发往");
		fieldHead.add("内容");
		fieldHead.add("已读");
		fieldHead.add("状态");
		fieldHead.add("创建日期日期");
		fieldHead.add("创建日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TMail mailBean : mailList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(mailBean.getMalNo());
			fieldItem.add(mailBean.getMerNo());
			fieldItem.add(mailBean.getMalInfo());
			fieldItem.add((mailBean.getMalRead().equals("0")) ? "无效" : "有效");
			fieldItem.add((mailBean.getMalState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(mailBean.getMalDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(mailBean.getMalDate(), DateUtil.TIME));
			fieldItem.add(mailBean.getMalRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IMailService getMailService() {
		return mailService;
	}

	public void setMailService(IMailService mailService) {
		this.mailService = mailService;
	}

	public TMail getMailBean() {
		return mailBean;
	}

	public void setMailBean(TMail mailBean) {
		this.mailBean = mailBean;
	}

	public List<TMail> getMailList() {
		return mailList;
	}

	public void setMailList(List<TMail> mailList) {
		this.mailList = mailList;
	}

	public String getMalNo() {
		return malNo;
	}

	public void setMalNo(String malNo) {
		this.malNo = malNo;
	}

	public String getMalNos() {
		return malNos;
	}

	public void setMalNos(String malNos) {
		this.malNos = malNos;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public String getMalInfo() {
		return malInfo;
	}

	public void setMalInfo(String malInfo) {
		this.malInfo = malInfo;
	}

	public String getMalRead() {
		return malRead;
	}

	public void setMalRead(String malRead) {
		this.malRead = malRead;
	}

	public String getMalState() {
		return malState;
	}

	public void setMalState(String malState) {
		this.malState = malState;
	}

	public Date getMalDate() {
		return malDate;
	}

	public void setMalDate(Date malDate) {
		this.malDate = malDate;
	}

	public Date getMalDateTop() {
		return malDateTop;
	}

	public void setMalDateTop(Date malDateTop) {
		this.malDateTop = malDateTop;
	}

	public Date getMalDateEnd() {
		return malDateEnd;
	}

	public void setMalDateEnd(Date malDateEnd) {
		this.malDateEnd = malDateEnd;
	}

	public String getMalRemark() {
		return malRemark;
	}

	public void setMalRemark(String malRemark) {
		this.malRemark = malRemark;
	}

}
